package com.qire.manhua.dialog.popup;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.support.base.BasePopupMenu;
import com.qire.manhua.databinding.LayoutPickerMenuBinding;
import com.qire.manhua.view.picker.PickerView.ISelectedListener;
import com.qire.manhua.view.picker.PickerViewAdapter;

public class PickerMenu extends BasePopupMenu {

    private final LayoutPickerMenuBinding viewDataBinder;

    public PickerMenu(Context context) {
        super(context);
        viewDataBinder = LayoutPickerMenuBinding.inflate(LayoutInflater.from(context));
        setContentView(viewDataBinder.getRoot());

        viewDataBinder.buttonCancel.setOnClickListener((View view) -> dismiss());
        viewDataBinder.buttonSubmit.setOnClickListener((View view) -> dismiss());

    }

    public void showBottomAt(View view){
        showAtLocation(view, Gravity.BOTTOM,0,0);
    }

    public PickerMenu setTitle(String title){
        viewDataBinder.textTitle.setText(title);
        return this;
    }

    public PickerMenu setCancelButtonOnClickListener(OnClickListener listener) {
        if(ObjectUtil.nonNull(listener)) {
            viewDataBinder.buttonCancel.setOnClickListener((View view) -> {
                listener.onClick(view);
                dismiss();
            });
        }
        return this;
    }
    public PickerMenu setSubmitButtonOnClickListener(OnClickListener listener) {
        if(ObjectUtil.nonNull(listener)) {
            viewDataBinder.buttonSubmit.setOnClickListener((View view) -> {
                listener.onClick(view);
                dismiss();
            });
        }
        return this;
    }

    public PickerMenu setPickerViewBefore(PickerViewAdapter adapter){
        viewDataBinder.pickerViewBefore.setAdapter(adapter);
        viewDataBinder.pickerViewBefore.setVisibility(View.VISIBLE);
        return this;
    }
    public PickerMenu setPickerViewMiddle(PickerViewAdapter adapter){
        viewDataBinder.pickerViewMiddle.setAdapter(adapter);
        viewDataBinder.pickerViewMiddle.setVisibility(View.VISIBLE);
        return this;
    }
    public PickerMenu setPickerViewAfter(PickerViewAdapter adapter){
        viewDataBinder.pickerViewAfter.setAdapter(adapter);
        viewDataBinder.pickerViewAfter.setVisibility(View.VISIBLE);
        return this;
    }
    public <T, A extends PickerViewAdapter<T>> PickerMenu setBeforeOnSelectedListener(ISelectedListener<T, A> listener){
        viewDataBinder.pickerViewBefore.setOnSelectedListener(listener);
        return this;
    }
    public <T, A extends PickerViewAdapter<T>> PickerMenu setMiddleOnSelectedListener(ISelectedListener<T, A> listener){
        viewDataBinder.pickerViewMiddle.setOnSelectedListener(listener);
        return this;
    }
    public <T, A extends PickerViewAdapter<T>> PickerMenu setAfterOnSelectedListener(ISelectedListener<T, A> listener){
        viewDataBinder.pickerViewAfter.setOnSelectedListener(listener);
        return this;
    }

    public PickerMenu reset() {
        viewDataBinder.textTitle.setText("");
        viewDataBinder.pickerViewBefore.reset();
        viewDataBinder.pickerViewBefore.setVisibility(View.GONE);
        viewDataBinder.pickerViewMiddle.reset();
        viewDataBinder.pickerViewMiddle.setVisibility(View.GONE);
        viewDataBinder.pickerViewAfter.reset();
        viewDataBinder.pickerViewAfter.setVisibility(View.GONE);
        return this;
    }

}
